package com.castle.fortress.admin.exchange.service.impl;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.exchange.entity.ExchangeConfigEntity;
import com.castle.fortress.admin.exchange.dto.ExchangeConfigDto;
import com.castle.fortress.admin.exchange.mapper.ExchangeConfigMapper;
import com.castle.fortress.admin.exchange.service.ExchangeConfigService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 兑换中心设置 服务实现类
 *
 * @author whc
 * @since 2022-12-05
 */
@Service
public class ExchangeConfigServiceImpl extends ServiceImpl<ExchangeConfigMapper, ExchangeConfigEntity> implements ExchangeConfigService {
    /**
    * 获取查询条件
    * @param exchangeConfigDto
    * @return
    */
    public QueryWrapper<ExchangeConfigEntity> getWrapper(ExchangeConfigDto exchangeConfigDto){
        QueryWrapper<ExchangeConfigEntity> wrapper= new QueryWrapper();
        if(exchangeConfigDto != null){
            ExchangeConfigEntity exchangeConfigEntity = ConvertUtil.transformObj(exchangeConfigDto,ExchangeConfigEntity.class);
            wrapper.like(StrUtil.isNotEmpty(exchangeConfigEntity.getName()),"name",exchangeConfigEntity.getName());
            wrapper.eq(exchangeConfigEntity.getCreateUser() != null,"create_user",exchangeConfigEntity.getCreateUser());
            wrapper.eq(exchangeConfigEntity.getCreateTime() != null,"create_time",exchangeConfigEntity.getCreateTime());
            wrapper.orderByDesc("create_time");
        }
        return wrapper;
    }


    @Override
    public IPage<ExchangeConfigDto> pageExchangeConfig(Page<ExchangeConfigDto> page, ExchangeConfigDto exchangeConfigDto) {
        QueryWrapper<ExchangeConfigEntity> wrapper = getWrapper(exchangeConfigDto);
        Page<ExchangeConfigEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<ExchangeConfigEntity> exchangeConfigPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<ExchangeConfigDto> pageDto = new Page(exchangeConfigPage.getCurrent(), exchangeConfigPage.getSize(),exchangeConfigPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(exchangeConfigPage.getRecords(),ExchangeConfigDto.class));
        return pageDto;
    }

    @Override
    public IPage<ExchangeConfigDto> pageExchangeConfigExtends(Page<ExchangeConfigDto> page, ExchangeConfigDto exchangeConfigDto){
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        ExchangeConfigEntity exchangeConfigEntity = ConvertUtil.transformObj(exchangeConfigDto,ExchangeConfigEntity.class);
        List<ExchangeConfigEntity> exchangeConfigList=baseMapper.extendsList(pageMap,exchangeConfigEntity);
        Long total = baseMapper.extendsCount(exchangeConfigEntity);
        Page<ExchangeConfigDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(exchangeConfigList,ExchangeConfigDto.class));
        return pageDto;
    }
    @Override
    public ExchangeConfigDto getByIdExtends(Long id){
        ExchangeConfigEntity  exchangeConfigEntity = baseMapper.getByIdExtends(id);
        return ConvertUtil.transformObj(exchangeConfigEntity,ExchangeConfigDto.class);
    }

    @Override
    public List<ExchangeConfigDto> listExchangeConfig(ExchangeConfigDto exchangeConfigDto){
        QueryWrapper<ExchangeConfigEntity> wrapper = getWrapper(exchangeConfigDto);
        List<ExchangeConfigEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,ExchangeConfigDto.class);
    }
}

